<?php
class Events
{
	private static $listeners = array();
	
	public static function addEventListener($aEventType, $aModule)
	{
		foreach (self::$listeners as $listener)
		{
			if ($listener->event == $aEventType && $listener->module == $aModule)
				return;
		}
		
		self::$listeners[] = new EventListenerObject($aEventType, $aModule);
	}
	
	public static function removeEventListener($aEventType, $aModule)
	{
		$newListeners = array();
		foreach (self::$listeners as $listener)
		{
			if (!($listener->event == $aEventType && $listener->module == $aModule))
				$newListeners[] = $listener;
		}
		
		$this->listeners = $newListeners;
	}
	
	public static function dispatchEvent($aEvent)
	{
		foreach (self::$listeners as $listener)
		{
			if ($listener->event == $aEvent->type)
			{
				$theModule = ModuleController::getModule($listener->module);
				$theModule->run($aEvent);
			}
		}
	}
}
?>